package com.eight.group.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.eight.group.entity.PageResult;
import com.eight.group.mapper.OrderMapper;
import com.eight.group.mapper.SiteMapper;
import com.eight.group.pojo.Order;
import com.eight.group.pojo.Setmeal;
import com.eight.group.pojo.Site;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xqx.eight.group.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SiteService.class)
@Transactional
public class SiteServiceImpl implements SiteService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SiteMapper sitemapper;
    /**
     * 查询所有地址的方法
     * @return
     */
    @Override
    public List<Site> findAll() {

        return sitemapper.findAll();
    }

    /**
     * 分页条件查询的方法
     * @param currentPage   当前页
     * @param pageSize      每页显示数据量
     * @param queryString   查询条件
     * @return
     */
    @Override
    public PageResult findPage(String currentPage, String pageSize, String queryString) {
        PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        List<Site> list = sitemapper.findPage(queryString);
        return new PageResult(new PageInfo<>(list).getTotal(),list);
    }

    /**
     * 删除地址的方法
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        //1 通过id查询关联的订单
        List<Order> orders = orderMapper.findBySiteId(id);
        //2 对查询的结果进行判断
        if (orders == null || orders.size()==0){
            //5 没有关联订单 调用方法删除
            try {
                sitemapper.deleteById(id);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            //6 返回true
            return true;
        }else {
            //查询结果不为空
            return false;
        }
    }

    /**
     * 添加地址的方法
     * @param site
     */
    public void addAddress(Site site) {
        sitemapper.addAddress(site);
    }
}
